package com.sm.controller;

import java.util.List;

import com.sm.model.Activities;
import com.sm.model.UserActivities;
import com.sm.service.ActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sm.service.UserActivitiesService;
import com.sm.util.CommonUtil;
import com.sm.util.Msg;

@Controller
@RequestMapping("/userActivities")
public class UserActivitiesController {

    @Autowired
    private UserActivitiesService userActivitiesService;

    @Autowired
    private ActivitiesService activitiesService;


    /**
     * 查询过期参会券
     * @return
     */
    @RequestMapping("/getUserActivities")
    @ResponseBody
    public Msg getUserActivitiesBefore() {

        UserActivities userActivitiesBefore = userActivitiesService.getUserActivitiesBefore();

        return Msg.success().add("userActivitiesBefore",userActivitiesBefore);
    }
    /**
     * 用户报名
     *
     * @param userId
     * @param activitiesId
     * @return
     */
    @RequestMapping("/addUserActivities")
    @ResponseBody
    public Msg addUserActivities(Integer activitiesId, Integer userId) {
        if (activitiesId == null || userId == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }
        UserActivities ua = new UserActivities();
        ua.setActivitiesCode(CommonUtil.timeMillion() + String.valueOf(userId));
        ua.setActivitiesId(activitiesId);
        ua.setUserId(userId);
        ua.setUserActivitiesStatus(0);
        int a = userActivitiesService.insertSelective(ua);
        if (a == 1) {
            return Msg.success().add("msg", "报名成功！");
        } else {
            return Msg.fail().add("msg", "报名失败！");
        }
    }

    /**
     * 查询近期参会券
     *
     * @return
     */
    @RequestMapping("/selectUserActivitiesNow")
    @ResponseBody
    public Msg selectActivitiesByTime() {
        List<UserActivities> userActivities = userActivitiesService.getUserActivitiesNow();
        return Msg.success().add("userActivities", userActivities);
    }

    /**
     * 查询参会券详情
     * @param
     * @return
     */
    @RequestMapping("/selectActivitiesById")
    @ResponseBody
    public Msg selectActivitiesById(@RequestParam("userActivitiesId") Integer userActivitiesId) {
        UserActivities userActivities = userActivitiesService.selectActivitiesById(userActivitiesId);
        return Msg.success().add("userActivities", userActivities);
    }


}
